const userModels = require('../models/User')

class userService{
    async findUser(username){
        const result = await userModels.findUser(username);
        if(!result){
            throw new Error('没有查询到数据');
        }
        return result;
    }

    async cerateUser(data){
        if(!data){
            throw new Error('插入数据为空！');
        }
        const result = await userModels.cerateUser(data);
        return result;
    }

    async login(data){
        if(!data){
            throw new Error('请求数据为空');
        }
        const { password } = data;
        const result = await userModels.login(data);
        if(!result){
            throw new Error ("用户不存在");
        }
        if(result.password !== password){
            throw new Error ("密码错误");
        }
        const { company_id,name } = result;
        return { company_id,name };
    }
}

module.exports = new userService();